
package com.probiz.estore.customer.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.probiz.estore.common.model.customer.Wishlist;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.core.model.BaseObject;
import com.probiz.estore.customer.dao.WishlistDao;

/**
 * Wishlist Data Access Object (DAO) implementation. Developer introduced
 * interfaces should be declared here. Won't get overwritten.
 */
public class WishlistDaoImpl extends HibernateGenericDaoImpl<Wishlist> implements WishlistDao {

	@Override
	protected void initFilterHqlMap() {
		filterHqlMap.put(BaseObject.FilterHqlName.DEFAULT, "select s from Wishlist s");
		filterHqlMap.put(Wishlist.FilterHqlName.FILTER_BY_CUSTOMER, "select s from Wishlist s where s.customer.appuserId = ?");
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.customer.dao.WishlistDao#getByCustomerId(java.lang.Integer)
	 */
	public List<Wishlist> getByCustomerId(Integer customerId) {
		String hql = "from Wishlist where customer.appuserId=?";
		return findByHql(hql, customerId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.customer.dao.WishlistDao#getCustomerDefaultWishlist(java.lang.Integer)
	 */
	public Wishlist getCustomerDefaultWishlist(Integer customerId) {
		String hql = "from Wishlist where customer.appuserId=:appuserId and isDefault=:isDefault";
		Query query = getSession().createQuery(hql);

		query.setInteger("appuserId", customerId.intValue());
		query.setShort("isDefault", Wishlist.DEFAULT_LIST.shortValue());

		List list = query.list();
		Wishlist wishlist = null;
		if (list.size() > 0) {
			wishlist = (Wishlist) list.get(0);
		} else {
			hql = "from Wishlist where customer.appuserId=:appuserId order by wishlistId asc ";
			query = getSession().createQuery(hql);
			query.setInteger("appuserId", customerId);
			list = query.list();
			if (list.size() > 0) {
				return (Wishlist) list.get(0);
			}
		}
		return wishlist;
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.WishlistDao#updateDefaultWishList(java.lang.Integer, java.lang.Integer)
	 */
	public void updateDefaultWishList(Integer wishlistId, Integer customerId) {
		String hql = "update Wishlist set isDefault =? where customer.appuserId=? and wishlistId<>?";
		Query query = getSession().createQuery(hql);
		query.setShort(0, Wishlist.NOT_DEFAULT_LIST.shortValue());
		query.setInteger(1, customerId.intValue());
		query.setInteger(2, wishlistId.intValue());
		query.executeUpdate();

		hql = "update Wishlist set isDefault=? where customer.appuserId=? and wishlistId=?";
		query = getSession().createQuery(hql);
		query.setShort(0, Wishlist.DEFAULT_LIST.shortValue());
		query.setInteger(1, customerId.intValue());
		query.setInteger(2, wishlistId.intValue());
		query.executeUpdate();

	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.WishlistDao#deleteWishlist(java.lang.Integer)
	 */
	public void deleteWishlist(Integer wishlistId) {

		String hql = "delete WishlistItem where wishlist.wishListId=?";

		Query query = getSession().createQuery(hql);
		query.setInteger(0, wishlistId.intValue());
		query.executeUpdate();

		hql = "delete Wishlist where wishListId=?";

		query = getSession().createQuery(hql);
		query.setInteger(0, wishlistId.intValue());
		query.executeUpdate();

		getSession().flush();
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.dao.WishlistDao#isProductExistInWishList(java.lang.Integer, java.lang.Integer)
	 */
	public boolean isProductExistInWishList(Integer wishlistId,
			Integer productId) {
		if (wishlistId.equals(Integer.valueOf(0))) {
			return false;
		}
		String hql = "select count(*) from WishlistItem vo where vo.wishlist.wishListId=? and vo.product.productId=? ";
		Query query = getSession().createQuery(hql);
		query.setInteger(0, wishlistId.intValue());
		query.setInteger(1, productId.intValue());
		Long count = (Long) query.uniqueResult();

		if (count.equals(Long.valueOf(0))) {
			return false;
		} else {
			return true;
		}
	}

}